package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.CustomerShareDAO;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.CustomerShare;
import com.feeyin.radar.crm.service.CustomerShareService;
import com.feeyin.radar.sys.dao.DepartmentDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.Department;
import com.feeyin.radar.sys.model.Users;
import java.util.ArrayList;
import java.util.List;

public class CustomerShareServiceImpl
  implements CustomerShareService
{
  private CustomerShareDAO customerShareDAO;
  private UserDAO userDAO;
  private DepartmentDAO departmentDAO;
  private CustomerDAO customerDAO;

  public CustomerShareDAO getCustomerShareDAO()
  {
    return this.customerShareDAO;
  }

  public void setCustomerShareDAO(CustomerShareDAO customerShareDAO) {
    this.customerShareDAO = customerShareDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public DepartmentDAO getDepartmentDAO() {
    return this.departmentDAO;
  }

  public void setDepartmentDAO(DepartmentDAO departmentDAO) {
    this.departmentDAO = departmentDAO;
  }

  public CustomerDAO getCustomerDAO() {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public List getActivityCustomerSharesByCustomerId(Integer customerId) {
    List list = new ArrayList();
    list = this.customerShareDAO.getActivityCustomerSharesByCustomerId(customerId);
    if (list != null) {
      for (int i = 0; i < list.size(); i++) {
        CustomerShare customerShare = new CustomerShare();
        customerShare = (CustomerShare)list.get(i);
        Users user = new Users();
        user = this.userDAO.getUser(customerShare.getSalePersonId());
        if (user != null) {
          customerShare.setSalePerson(user.getUserName());
          Department department = new Department();
          department = this.departmentDAO.getDepartment(user.getDeptId());
          if (department != null) {
            customerShare.setDeptName(department.getDeptName());
          }
        }
      }
    }
    return list;
  }

  public boolean insertCustomerShare(CustomerShare customerShare) {
    if (customerShare == null) return false;
    if (customerShare.getUserId() == null) return false;

    for (int i = 0; i < customerShare.getUserId().length; i++) {
      if (this.customerShareDAO.getCustomerShareByCustomerIdAndSalePersonId(customerShare.getCustomerId(), customerShare.getUserId()[i]) != null) {
        CustomerShare cShare = new CustomerShare();
        cShare = new CustomerShare();
        cShare.setCustomerId(customerShare.getCustomerId());
        cShare.setSalePersonId(customerShare.getUserId()[i]);
        cShare.setIsCurShare("1");
        cShare.setIsReadWrite(customerShare.getIsReadWrite());
        this.customerShareDAO.updateCustomerShare(cShare);
      } else {
        customerShare.setSalePersonId(customerShare.getUserId()[i]);
        customerShare.setIsCurShare("1");
        this.customerShareDAO.insertCustomerShare(customerShare);
      }
    }

    Customer customer = new Customer();
    customer.setId(customerShare.getCustomerId());
    customer.setIsShare("1");
    this.customerDAO.updateCustomer(customer);
    return true;
  }

  public boolean deleteCustomerShare(CustomerShare customerShare) {
    if (customerShare == null) return false;
    if (customerShare.getUserId() == null) return false;

    for (int i = 0; i < customerShare.getUserId().length; i++) {
      customerShare.setSalePersonId(customerShare.getUserId()[i]);
      customerShare.setIsCurShare("0");
      this.customerShareDAO.updateCustomerShare(customerShare);
    }

    List list = new ArrayList();
    list = this.customerShareDAO.getActivityCustomerSharesByCustomerId(customerShare.getCustomerId());
    Customer customer = new Customer();
    if ((list != null) && (list.size() == 1)) {
      customer = this.customerDAO.getCustomerById(customerShare.getCustomerId());
      CustomerShare cs = new CustomerShare();
      cs = (CustomerShare)list.get(0);
      if ((customer != null) && (cs != null) && 
        (cs.getSalePersonId().compareTo(customer.getSalePersonId()) == 0))
      {
        customer = new Customer();
        customer.setId(customerShare.getCustomerId());
        customer.setIsShare("0");
        this.customerDAO.updateCustomer(customer);
      }
    }

    return true;
  }
}